<template>
  <view class="realname-container">
    <!-- 顶部说明 -->
    <view class="header-section">
      <view class="header-desc">请上传本人身份证正反面照片，认证通过后将无法修改</view>
    </view>
    <!-- 认证表单 -->
    <view class="form-section">
      <view class="form-item">
        <view class="form-label">身份证正面</view>
        <view class="upload-box" @click="uploadIdFront">
          <image v-if="idFront" :src="idFront" class="id-img" mode="aspectFit" />
          <view v-else class="upload-placeholder">点击上传身份证正面</view>
        </view>
      </view>
      <view class="form-item">
        <view class="form-label">身份证反面</view>
        <view class="upload-box" @click="uploadIdBack">
          <image v-if="idBack" :src="idBack" class="id-img" mode="aspectFit" />
          <view v-else class="upload-placeholder">点击上传身份证反面</view>
        </view>
      </view>
    </view>
    <!-- 协议同意 -->
    <view class="agreement-section">
      <view class="agreement-item" @click="toggleAgreement">
        <view class="checkbox" :class="{ 'checked': isAgreed }">
          <text v-if="isAgreed" class="check-icon">✓</text>
        </view>
        <text class="agreement-text">我已阅读并同意</text>
        <text class="agreement-link" @click.stop="showAgreement">《实名认证服务协议》</text>
      </view>
    </view>
    <!-- 提交按钮 -->
    <view class="submit-section">
      <button class="submit-btn" @click="handleSubmit" :disabled="!canSubmit">
        提交认证
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
const idFront = ref('')
const idBack = ref('')
const isAgreed = ref(false)
const canSubmit = computed(() => idFront.value && idBack.value && isAgreed.value)

onMounted(() => {
  const status = uni.getStorageSync('realnameStatus')
  if (status === '已认证') {
    uni.showToast({ title: '已认证，无需重复认证', icon: 'none' })
    setTimeout(() => uni.navigateBack(), 1200)
  }
  // 未通过或未认证可继续
})

function uploadIdFront() {
  uni.chooseImage({
    count: 1,
    sizeType: ['compressed'],
    sourceType: ['album', 'camera'],
    success: (res) => {
      idFront.value = res.tempFilePaths[0]
    }
  })
}
function uploadIdBack() {
  uni.chooseImage({
    count: 1,
    sizeType: ['compressed'],
    sourceType: ['album', 'camera'],
    success: (res) => {
      idBack.value = res.tempFilePaths[0]
    }
  })
}
function toggleAgreement() {
  isAgreed.value = !isAgreed.value
}
function showAgreement() {
  uni.showModal({
    title: '实名认证服务协议',
    content: '1. 您承诺上传的身份证照片真实有效\n2. 认证通过后将无法修改\n3. 我们将严格保护您的隐私信息\n4. 认证信息仅用于身份验证',
    showCancel: false
  })
}
function handleSubmit() {
  if (!canSubmit.value) {
    uni.showToast({ title: '请上传身份证正反面并同意协议', icon: 'none' })
    return
  }
  uni.showLoading({ title: '认证中...' })
  setTimeout(() => {
    uni.hideLoading()
    // 模拟认证结果
    const isSuccess = Math.random() > 0.5 // 随机成功/失败
    if (isSuccess) {
      uni.setStorageSync('realnameStatus', '已认证')
      uni.showModal({
        title: '认证成功',
        content: '您的实名认证已通过，信息将用于合同签署',
        showCancel: false,
        success: () => {
          uni.navigateBack({
            success: () => {
              uni.showToast({ title: '认证成功', icon: 'success' })
            }
          })
        }
      })
    } else {
      uni.setStorageSync('realnameStatus', '未通过')
      uni.showModal({
        title: '认证失败',
        content: '您的实名认证未通过，请重新上传清晰的身份证照片',
        showCancel: false
      })
    }
  }, 2000)
}
</script>

<style scoped>
.realname-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20rpx;
}

/* 顶部说明 */
.header-section {
  background-color: #fff;
  border-radius: 20rpx;
  padding: 40rpx 30rpx;
  margin-bottom: 20rpx;
  text-align: center;
}

.header-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
}

.header-desc {
  font-size: 28rpx;
  color: #666;
  line-height: 1.5;
}

/* 表单区域 */
.form-section {
  background-color: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
}

.form-item {
  margin-bottom: 30rpx;
}

.form-item:last-child {
  margin-bottom: 0;
}

.form-label {
  font-size: 30rpx;
  color: #333;
  margin-bottom: 20rpx;
  font-weight: 500;
}

.form-input {
  width: 100%;
  height: 80rpx;
  border: 2rpx solid #e0e0e0;
  border-radius: 10rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
  color: #333;
  background-color: #fff;
  box-sizing: border-box;
}

.form-input:focus {
  border-color: #f55a4e;
}



/* 协议区域 */
.agreement-section {
  background-color: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 40rpx;
}

.agreement-item {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.checkbox {
  width: 40rpx;
  height: 40rpx;
  border: 2rpx solid #ddd;
  border-radius: 6rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
}

.checkbox.checked {
  background-color: #f55a4e;
  border-color: #f55a4e;
}

.check-icon {
  color: #fff;
  font-size: 24rpx;
  font-weight: bold;
}

.agreement-text {
  font-size: 28rpx;
  color: #666;
}

.agreement-link {
  font-size: 28rpx;
  color: #f55a4e;
}

/* 提交按钮 */
.submit-section {
  padding: 0 20rpx;
}

.submit-btn {
  width: 100%;
  height: 88rpx;
  background-color: #f55a4e;
  color: #fff;
  border: none;
  border-radius: 44rpx;
  font-size: 32rpx;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
}

.submit-btn:disabled {
  background-color: #ccc;
}

.submit-btn::after {
  border: none;
}

.upload-box {
  width: 100%;
  height: 200rpx;
  border: 2rpx dashed #e0e0e0;
  border-radius: 10rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fafafa;
  margin-bottom: 10rpx;
  cursor: pointer;
}
.upload-placeholder {
  color: #bbb;
  font-size: 28rpx;
}
.id-img {
  width: 90%;
  height: 180rpx;
  object-fit: contain;
  border-radius: 8rpx;
}
</style> 